clear all
set more off
set scheme s2color

** Estimate gap in travel dist and time when crossing dep border

global input "...\replication\data"
global output "...\replication\outputs"

foreach i in all reg {
// Import distance matrix
import dbase "$input\lines_`i'_network.dbf", clear
keep OriginID Destinatio Total_Leng
ren OriginID arcgis_i
ren Destinatio arcgis_j
gen trav_dist = round(Total_Leng,0.01)

merge m:1 arcgis_i using com_pt_5km_NonNat.dta
keep if _merge == 3
drop _merge

merge m:1 arcgis_j using dep_cen.dta
keep if _merge == 3
drop _merge


merge 1:1 arcgis_i arcgis_j using dist_border_reg_only.dta
keep if _merge == 3
drop _merge

****************** Get dist instrumental border ******************
merge m:1 com using "$input\com_dist_instr.dta"
drop _merge

merge m:1 com using "$input\com_arcgis.dta"
drop _merge

replace dist_border_line = -dist_border_line if dist_border != 0
replace dist_instr = -dist_instr if dist_border != 0

** Drop those close to nat features
keep if dist_river >= 3 | dist_river < 0
gen montain = 0
foreach i in 64 65 66 31 9 5 73 74 63 43 15 {
replace montain = 1 if dep_i == `i'
replace montain = 1 if dep_j== `i'
}
drop if montain == 1

gen abs_dist_border_line = abs(dist_border_line)


****************** calonico et al: Travel distances ******************
foreach i in 0.5 1 1.5 2 5 10  {
rdrobust trav_dist dist_border_line , c(0) covs(dep_i dep_j dist_river dist_nat_border) h(`i') 
local j = 10*`i'
est store ATE_`j'
}

coefplot  ATE_5 || ATE_10 || ATE_15 || ATE_20 || ATE_50 || ATE_100  , color(black) ciopts(recast(. rcap) color(black black) lwidth(*0.75 *1.5)) ///
vertical omitted  yline(0, lcolor(red)) levels(95 90) ///
bycoefs  ///
xlabel(1 "500m" 2 "1km" 3 "Opt. = 1.39km" 4 "2km" 5 "5km" 6 "10km") ///
ms(m) color(black)  graphregion(color(white)) bgcolor(white) ///
msize(small) ytitle("RD estimate: Travel distance (km)") ///
title("") xtitle("Bandwidth") legend(off) ///
note("Estimate with optimal bandwidth: -7.579***(2.614). Implied penalty: 4.7km.")
graph export "$output\trav_dist_bandwidths.pdf", replace


****************** calonico et al : Travel time ******************
gen trav_time = 60*(trav_dist/90)
foreach i in 0.5 1 1.5 2 5 10  {
rdrobust trav_time dist_border_line , c(0) covs(dep_i dep_j dist_river dist_nat_border) h(`i') 
local j = 10*`i'
est store ATE_`j'
}

coefplot  ATE_5 || ATE_10 || ATE_15 || ATE_20 || ATE_50 || ATE_100  , color(black) ciopts(recast(. rcap) color(black black) lwidth(*0.75 *1.5)) ///
vertical omitted  yline(0, lcolor(red)) levels(95 90) ///
bycoefs  ///
xlabel(1 "500m" 2 "1km" 3 "Opt. = 1.39km" 4 "2km" 5 "5km" 6 "10km") ///
ms(m) color(black)  graphregion(color(white)) bgcolor(white) ///
msize(small) ytitle("RD estimate: Travel time (min)") ///
title("") xtitle("Bandwidth") legend(off) 
graph export "$ouput\trav_time_bandwidths.pdf", replace
